package com.tsingteng.cosfun.ui.found;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BaseActivity;
import com.tsingteng.cosfun.callback.ActivityJavascriptInterface;
import com.tsingteng.cosfun.common.Constant;
import com.tsingteng.cosfun.helper.ActivityManagers;
import com.tsingteng.cosfun.utils.StringUtils;


import butterknife.BindView;

public class FoundWebViewActivity extends BaseActivity {
    @BindView(R.id.found_activity_wv)
    WebView mFoundActivityWv;
    @BindView(R.id.myProgressBars)
    ProgressBar mProgressBar;
    long mId;
    String url;
   private ActivityJavascriptInterface mInterface;
   private boolean canFound = false;
    @Override
    protected void initView() {
        ActivityManagers.getInstance().addFoundActivity(this);
        StringUtils.setWebSettings(mFoundActivityWv, this);
        mFoundActivityWv.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (null == mProgressBar) {
                    return;
                }
                if (newProgress == 100) {
                    mProgressBar.setVisibility(View.GONE);
                } else {
                    mProgressBar.setVisibility(View.VISIBLE);
                    mProgressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        mFoundActivityWv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                canFound = true;
                super.onPageFinished(view, url);
            }
        });
        mInterface = new ActivityJavascriptInterface(this);
        mFoundActivityWv.addJavascriptInterface(mInterface, "cosfun");
        mFoundActivityWv.loadUrl(url + mId);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_found_web_view;
    }

    @Override
    protected void getIntentData(Intent intent) {
        isFound = true;
        mId = intent.getLongExtra(Constant.ACTIVITYID, 0);
        url= intent.getStringExtra(Constant.URL);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
       return super.onKeyDown(keyCode,event);
    }
}
